using System;
using System.Runtime.InteropServices;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Support.Enums;
using MS.InformationManagement.WP.DocumentCenter.UserAdministration;
using MS.InformationManagement.WP.DocumentCenter.UserProfileCreation;
using MS.InformationManagement.WP.RecordCenter.BackdropMassUploader;
using MS.InformationManagement.WP.RecordCenter.TRDConfigGrid;

namespace MS.InformationManagement.WP.Features.Document_Center_Webparts
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("04cbf4a8-7943-4db2-927d-3a81cff21e56")]
    public class Document_Center_WebpartsEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite currentSite = properties.Feature.Parent as SPSite;
            using (SPWeb currentWeb = currentSite.OpenWeb())
            {
                currentWeb.AllowUnsafeUpdates = true;

               
                //AddProfileManagementWebPartToPage("pages/gestionperfiles.aspx", "Zone 1", 0, currentSite.ID);
                //AddUserManagementWebPartToPage("pages/administracionusuarios.aspx", "Zone 1", 0, currentSite.ID);
                //AddTRDManagementWebPartToPage("pages/trd.aspx", "Zone 1", 0, currentSite.ID);
                
                //AddListWebPartToPage("Roles", "pages/Roles.aspx", "Zone 1", 0, currentSite.ID);
                //AddListWebPartToPage("Contactos Externos", "pages/Contactos.aspx", "Zone 1", 0, currentSite.ID);
                
                //AddListWebPartToPage("Fondos", "pages/Fondos.aspx", "Zone 1", 0, currentSite.ID);
                //AddListWebPartToPage("Secciones", "pages/Secciones.aspx", "Zone 1", 0, currentSite.ID);
                //AddListWebPartToPage("Series", "pages/Series.aspx", "Zone 1", 0, currentSite.ID);
                //AddListWebPartToPage("Subseries", "pages/Subseries.aspx", "Zone 1", 0, currentSite.ID);
                //AddListWebPartToPage("Tipos Documentales", "pages/TiposDocumentales.aspx", "Zone 1", 0, currentSite.ID);

                //AddMassiveUploaderWebPartToPage(ListUploadOperation.Backdrop, "pages/cargamasivadefondos.aspx", "Zone 1", 0, currentSite.ID);
                //AddMassiveUploaderWebPartToPage(ListUploadOperation.Sections, "pages/cargamasivadesecciones.aspx", "Zone 1", 0, currentSite.ID);
                //AddMassiveUploaderWebPartToPage(ListUploadOperation.Series, "pages/cargamasivadeseries.aspx", "Zone 1", 0, currentSite.ID);
                //AddMassiveUploaderWebPartToPage(ListUploadOperation.Subseries, "pages/cargamasivadesubseries.aspx", "Zone 1", 0, currentSite.ID);
                //AddMassiveUploaderWebPartToPage(ListUploadOperation.People, "pages/cargamasivadepersonas.aspx", "Zone 1", 0, currentSite.ID);
                //AddMassiveUploaderWebPartToPage(ListUploadOperation.Entities, "pages/cargamasivadeentidades.aspx", "Zone 1", 0, currentSite.ID);

                currentWeb.AllowUnsafeUpdates = false;
                  
            }
        }

  

        public static void AddListWebPartToPage(string listName, string pageUrl, string zoneID, int zoneIndex, Guid siteID)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite overridenSecuritySite = new SPSite(siteID))
                {
                    using (SPWeb overridenSecurityWeb = overridenSecuritySite.OpenWeb())
                    {
                        SPList list = overridenSecurityWeb.Lists.TryGetList(listName);

                        // Create a webpart 
                        ListViewWebPart wp = new ListViewWebPart();
                        wp.ZoneID = zoneID;   // Replace this ith the correct zone on your page. 
                        wp.ListName = list.ID.ToString("B").ToUpper();
                        wp.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();
                        wp.ChromeType = PartChromeType.None;

                        //Checkout the page so you can modify it
                        SPFile page = overridenSecurityWeb.GetFile(pageUrl);
                        page.CheckOut();

                        // Get the web part collection 
                        SPLimitedWebPartManager coll = overridenSecurityWeb.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared);

                        // Add the web part 
                        coll.AddWebPart(wp, zoneID, zoneIndex);

                        //Check in the page again and publish
                        page.CheckIn(string.Empty, SPCheckinType.MajorCheckIn);
                    }
                }
            });
        }

        public static void AddProfileManagementWebPartToPage(string pageUrl, string zoneID, int zoneIndex, Guid siteID)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite overridenSecuritySite = new SPSite(siteID))
                {
                    using (SPWeb overridenSecurityWeb = overridenSecuritySite.OpenWeb())
                    {
                        // Create a webpart 
                        UserProfileCreation wp = new UserProfileCreation();
                        wp.ChromeType = PartChromeType.None;

                        //Checkout the page so you can modify it
                        SPFile page = overridenSecurityWeb.GetFile(pageUrl);
                        page.CheckOut();

                        // Get the web part collection 
                        SPLimitedWebPartManager coll = overridenSecurityWeb.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared);

                        // Add the web part 
                        coll.AddWebPart(wp, zoneID, zoneIndex);

                        //Check in the page again and publish
                        page.CheckIn(string.Empty, SPCheckinType.MajorCheckIn);
                    }
                }
            });
        }

        public static void AddUserManagementWebPartToPage(string pageUrl, string zoneID, int zoneIndex, Guid siteID)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite overridenSecuritySite = new SPSite(siteID))
                {
                    using (SPWeb overridenSecurityWeb = overridenSecuritySite.OpenWeb())
                    {
                        // Create a webpart 
                        UserAdministration wp = new UserAdministration();
                        wp.ChromeType = PartChromeType.None;

                        //Checkout the page so you can modify it
                        SPFile page = overridenSecurityWeb.GetFile(pageUrl);
                        page.CheckOut();

                        // Get the web part collection 
                        SPLimitedWebPartManager coll = overridenSecurityWeb.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared);

                        // Add the web part 
                        coll.AddWebPart(wp, zoneID, zoneIndex);

                        //Check in the page again and publish
                        page.CheckIn(string.Empty, SPCheckinType.MajorCheckIn);
                    }
                }
            });
        }

        public static void AddTRDManagementWebPartToPage(string pageUrl, string zoneID, int zoneIndex, Guid siteID)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite overridenSecuritySite = new SPSite(siteID))
                {
                    using (SPWeb overridenSecurityWeb = overridenSecuritySite.OpenWeb())
                    {
                        // Create a webpart 
                        TRDConfigGrid wp = new TRDConfigGrid();
                        wp.ChromeType = PartChromeType.None;

                        //Checkout the page so you can modify it
                        SPFile page = overridenSecurityWeb.GetFile(pageUrl);
                        page.CheckOut();

                        // Get the web part collection 
                        SPLimitedWebPartManager coll = overridenSecurityWeb.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared);

                        // Add the web part 
                        coll.AddWebPart(wp, zoneID, zoneIndex);

                        //Check in the page again and publish
                        page.CheckIn(string.Empty, SPCheckinType.MajorCheckIn);
                    }
                }
            });
        }

        public static void AddMassiveUploaderWebPartToPage(ListUploadOperation operation, string pageUrl, string zoneID, int zoneIndex, Guid siteID)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite overridenSecuritySite = new SPSite(siteID))
                {
                    using (SPWeb overridenSecurityWeb = overridenSecuritySite.OpenWeb())
                    {
                        // Create a webpart 
                        BackdropMassUploader wp = new BackdropMassUploader();
                        wp.ChromeType = PartChromeType.None;
                        wp.Operation = operation;

                        //Checkout the page so you can modify it
                        SPFile page = overridenSecurityWeb.GetFile(pageUrl);
                        page.CheckOut();

                        // Get the web part collection 
                        SPLimitedWebPartManager coll = overridenSecurityWeb.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared);

                        // Add the web part 
                        coll.AddWebPart(wp, zoneID, zoneIndex);

                        //Check in the page again and publish
                        page.CheckIn(string.Empty, SPCheckinType.MajorCheckIn);
                    }
                }
            });
        }

        // Uncomment the method below to handle the event raised before a feature is deactivated.

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
